package edu.faytechcc.csc289.gui;

import edu.faytechcc.csc289.data.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;

/**
 * Login panel before main application
 *
 * 3/2014 CSC289
 *
 * @author William Robles
 */
class LoginView extends JPanel
{

    private final ArrayList<Employee> empList;

    private String position = "";

    private final ActionListener bL;
    private JPanel inputPanel, buttonPanel;
    private JTextField usernameTextField;
    private JPasswordField userPasswordField;
    private JLabel usernameLabel, userPasswordLabel;
    private JButton okButton,exitButton;
    
    private SimulacraDbConnection sDB = new SimulacraDbConnection();

    public LoginView(ActionListener bL)
    {
        this.empList = new ArrayList<>();
        this.bL = bL;
        this.setBorder(BorderFactory.createBevelBorder(100));
        this.setLayout(new GridLayout(2, 1));
        buildInputPanel();
        buildButtonPanel();
    }

    /**
     * buildInputPanel method builds the login name, password fields and labels
     */
    private void buildInputPanel()
    {
        JPanel temp = new JPanel();
        JPanel temp2 = new JPanel();
        JPanel temp3 = new JPanel();
        JPanel temp4 = new JPanel();

        inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(2, 2));

        usernameLabel = new JLabel("Enter Username:");
        userPasswordLabel = new JLabel("Enter Password:");

        usernameTextField = new JTextField(10);
        userPasswordField = new JPasswordField(10);

        temp.add(usernameLabel);
        inputPanel.add(temp);
        temp2.add(usernameTextField);

        inputPanel.add(temp2);
        temp3.add(userPasswordLabel);
        inputPanel.add(temp3);
        temp4.add(userPasswordField);
        inputPanel.add(temp4);
        this.add(inputPanel);
    }

    /**
     * buildButtonPanel method creates the panel and buttons OK and Cancel
     */
    private void buildButtonPanel()
    {
        buttonPanel = new JPanel();
        buttonPanel.setLayout(new BorderLayout());

        JPanel temp = new JPanel();

        okButton = new JButton("OK");
        exitButton = new JButton("Exit");

        okButton.addActionListener(bL);
        exitButton.addActionListener(bL);
        
        temp.add(okButton);
        temp.add(exitButton);
        buttonPanel.add(temp, BorderLayout.SOUTH);
        this.add(buttonPanel);
    }

    /**
     * getEmployees method will get resultset and place username and password in
     * arraylist empList
     */
    public void getEmployees()
    {
        ResultSet results = sDB.getLoginInfo();
        
        try
        {
            results.last();
            int numOfResults = results.getRow();
            results.first();

            for (int i = 0; i < numOfResults; i++)
            {
                Employee emp = new Employee();
                emp.setUsername(results.getString("Username"));
                emp.setPosition(results.getString("Position"));
                emp.setPassword(results.getString("Password"));
                empList.add(emp);
                results.next();
            }
            
            sDB.getLoginInfo().close();
            results.close();

        }
        catch (SQLException ex)
        {
            System.err.println("ERROR: " + ex.getMessage());
            System.exit(24);
        }
    }

    /**
     * validateLogin method checks if username exists. Then verifies password
     *
     * @return boolean valid
     */
    public boolean validateLogin()
    {
        boolean valid = false;

        for (Employee emp : empList)
        {
            if (usernameTextField.getText().equals(emp.getUsername()))
            {
                char[] correctPassword = new char[emp.getPassword().length()];
                char[] inputPassword = userPasswordField.getPassword();

                for (int i = 0; i < emp.getPassword().length(); i++)
                {
                    correctPassword[i] = emp.getPassword().charAt(i);
                }

                if (inputPassword.length == correctPassword.length)
                {
                    position = emp.getPosition();
                    valid = Arrays.equals(inputPassword, correctPassword);
                }
                
                userPasswordField.setText("");

            }
        }

        if (!valid)
        {
            JOptionPane.showMessageDialog(null,
                    "Invalid Username or Password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
        }
        
        return valid;
    }

    /**
     * @return the position
     */
    public String getPosition()
    {
        return position;
    }

}
